package cn.shop.admin.controller;

import cn.shop.admin.service.BlacklistService;
import cn.shop.base.MyPage;
import cn.shop.transcation.blacklist.entity.Blacklist;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.constant.Constant;
import cn.shop.utils.enums.OperationType;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author : 李一涵
 * @date : Created in 2021-11-10 11:14
 * @description : 黑名单controller
 */
@RestController
@RequestMapping("/api/v1/blacklist")
public class BlacklistController {

	@Resource
	private BlacklistService blacklistService;


	/**
	 * 新增黑名单
	 *
	 * @param blacklist 黑名单
	 * @return 返回结果
	 */
	@PostMapping("insert")
	@OperationLog(desc = "新增黑名单", type = OperationType.CREATE)
	public ApiResult<Map<String, Object>> insert(@Validated @RequestBody Blacklist blacklist) {
		blacklist.setSource(1);
		return ApiResult.result(blacklistService.save(blacklist));
	}

	/**
	 * 更新黑名单
	 *
	 * @param blacklist 黑名单
	 * @return 返回结果
	 */
	@PutMapping("update")
	@OperationLog(desc = "修改黑名单", type = OperationType.UPDATE)
	public ApiResult<Map<String, Object>> update(@Validated @RequestBody Blacklist blacklist) {
		return ApiResult.result(blacklistService.updateById(blacklist));
	}

	/**
	 * 获取黑名单列表
	 *
	 * @param blacklist 支付宝账号
	 * @return 返回结果
	 */
	@GetMapping("/queryByPage")
	public ApiResult<MyPage<Blacklist>> list(MyPage<Blacklist> page, Blacklist blacklist,
	                                         @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime startTime,
	                                         @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime endTime) {
		return ApiResult.success(blacklistService.page(page, Wrappers.lambdaQuery(blacklist)
				.apply(startTime != null,
						"date_format (create_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null,
						"date_format (create_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)));
	}

	/**
	 * 删除黑名单
	 *
	 * @param idList 编号
	 * @return 返回
	 */
	@DeleteMapping("/batchDelete")
	public ApiResult<Map<String, Object>> delete(@RequestParam("idList") List<Long> idList) {
		boolean isSuccess = blacklistService.deleteBlacklist(idList);
		return ApiResult.result(isSuccess);
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable Long id) {
		Blacklist blacklist = new Blacklist();
		blacklist.setId(id);
		return ApiResult.result(blacklistService.removeById(blacklist));
	}

	/**
	 * 获取黑明单信息
	 *
	 * @param id 编号
	 * @return 返回结果
	 */
	@GetMapping("/queryOne/{id}")
	public ApiResult<Blacklist> info(@PathVariable("id") Serializable id) {
		return ApiResult.success(blacklistService.getById(id));
	}

}
